
# -*- coding: utf-8 -*-

from django import forms
from django.forms.extras.widgets import SelectDateWidget
from django.forms import Select
from django.forms import ModelChoiceField
from django.utils.encoding import force_unicode
from comunidadcyt.Registration_Users.models import UserProfile
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User, Group
import datetime


SEX_CHOICE = (
    ('M','Masculino'),
    ('F','Femenino'),
)

COUNTRY_OPTS=   (('Paraguay','Paraguay'),
                 ('Argentina','Argentina'),
                 ('Brasil','Brasil'),
                 ('Uruguay','Uruguay'),
                 ('Chile','Chile'),
                 ('Venezuela','Venezuela'),
                 ('Bolivia','Bolivia'),
                )

TITLE_OPTS = (
    ('---', '---'),
    ('Lic.', 'Lic.'),
    ('Arq.', 'Arq.'),
    ('Ing.', 'Ing.'),
    ('M.Sc.', 'M.Sc.'),
    ('Ph.D.', 'Ph.D.')
)

class LoginForm(forms.Form):
    username = forms.CharField(max_length=100, label="Nombre de usuario")
    password = forms.CharField(widget=forms.PasswordInput, label="Contraseña")
    current_path_login = forms.CharField(widget=forms.HiddenInput)
    
class ProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        widgets = {'sexo':Select(None,choices = SEX_CHOICE),
                   'pais':Select(None,choices = COUNTRY_OPTS) ,
                   'fecha_nacimiento': SelectDateWidget(years = range(1950,datetime.date.today().year)),
                   }
        exclude = ('user')

class choiceName(ModelChoiceField):
    def label_from_instance(self, obj):
        name = force_unicode(obj.first_name +' '+ obj.last_name)
        return name

class CreateProfileForm(forms.ModelForm):
    def __init__(self, user=None, *args, **kwargs):
        super(CreateProfileForm, self).__init__(*args, **kwargs)
        if user:
            self.fields['user']=choiceName(queryset=User.objects.filter(pk = user.id))
            self.fields['user'].label='Usuario'
            self.fields['user'].empty_label=None


    class Meta:
        model = UserProfile
        widgets = {'sexo':Select(None,choices = SEX_CHOICE),
                   'pais':Select(None,choices = COUNTRY_OPTS) ,
                   'titulo':Select(None,choices = TITLE_OPTS) ,
                   'fecha_nacimiento': SelectDateWidget(years = range(1950,datetime.date.today().year)),
                   }
        exclude = ('cargo','motivo')

class UserForm(forms.ModelForm):
    def __init__(self, user=None, *args,**kwargs):
        super(UserForm,self).__init__(*args,**kwargs)
        self.fields['first_name'].label = "Nombre"
        self.fields['last_name'].label = "Apellido"
        self.fields['email'].label = "Email"

    class Meta:
        model = User
        fields = ('first_name','last_name','email')
            

class UserCreationFormExtended(UserCreationForm):
    motivo = forms.CharField(label="Motivo / Referencia", widget=forms.Textarea(attrs={'cols': 16, 'rows': 8}), max_length=500,required=True)

    def __init__(self,*args,**kwargs):
        super(UserCreationFormExtended,self).__init__(*args,**kwargs)
        self.fields['first_name'].label = "Nombre"
        self.fields['last_name'].label = "Apellido"
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True
        self.fields['email'].required = True
        
    class Meta:
        model = User
        fields = ('username','email','first_name','last_name')


class UserTypeForm(forms.ModelForm):
    class Meta:
        model = Group
        exclude = ('permissions')